#include"friendmodel.hpp"
#include"ConnectionPool.h"

void FriendModel::insert(int userid, int friendid)
{
    // 组装sql语句
    char sql[1024] = {0};
    sprintf(sql,"insert into Friend(userid,friendid) values(%d,%d)",
        userid,friendid);

    ConnectionPool *cp = ConnectionPool::getConnectionPool();

    shared_ptr<Connection> sp = cp->getConnection();
    sp->update(sql);
}

vector<User> FriendModel::query(int userid)
{
    // 组装sql语句
    char sql[1024] = {0};
    sprintf(sql,"select a.id,a.name,a.state from User a inner join Friend b on b.friendid = a.id where b.userid=%d",userid);

    vector<User> vec;

    ConnectionPool *cp = ConnectionPool::getConnectionPool();

    shared_ptr<Connection> sp = cp->getConnection();

    MYSQL_RES *res = sp->query(sql);
    if(res != nullptr)
    {
        // 获取userid的所有消息
        MYSQL_ROW row;
        while((row=mysql_fetch_row(res)) != nullptr)
        {
            User user;
            user.setId(atoi(row[0]));
            user.setName(row[1]);
            user.setState(row[2]);
            vec.push_back(user);
        }
        mysql_free_result(res);
        return vec;
    }


    return vec;
}
